package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.model.GlobalResponse;
import com.mall.consts.MallConst;
import com.mall.form.ShippingForm;
import com.mall.pojo.User;
import com.mall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Classname ShippingController
 * @Description
 * @Date 2022/7/31 18:32
 * @Created by TaoVh
 */
@RestController
public class ShippingController {

    private IShippingService shippingService;

    @Autowired
    public void setShippingService(IShippingService shippingService) {
        this.shippingService = shippingService;
    }

    @PostMapping("/shippings")
    public GlobalResponse add(@Valid @RequestBody ShippingForm form
            , HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.add(user.getId(), form);
    }

    @DeleteMapping("/shippings/{shippingId}")
    public GlobalResponse delete(@PathVariable Integer shippingId,HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.delete(user.getId(), shippingId);
    }

    @PutMapping("/shippings/{shippingId}")
    public GlobalResponse update(@PathVariable Integer shippingId
            ,HttpSession session,@Valid @RequestBody ShippingForm form){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.update(user.getId(), shippingId,form);
    }

    @GetMapping("/shippings")
    public GlobalResponse<PageInfo> list(@RequestParam(required = false,defaultValue = "1") Integer pageNum
            , @RequestParam(required = false,defaultValue = "10") Integer pageSize, HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.list(user.getId(), pageNum, pageSize);
    }

}
